var redis = require('redis');

var client = redis.createClient();
client.on("connect", function (err) {
        popFromQueue();
    });


function popFromQueue(){
	client.lpop('resque:queue:requests', handleResponse);
}

function handleResponse(err, result) {
  if (result == null) {
  	//setTimeout(function() { popFromQueue(); }, 100);
    setTimeout(popFromQueue, 100);
  } else {
    setTimeout(process(result), Math.random() * 10000);
  	popFromQueue();
  }
}

function process(result) {
    json = JSON.parse(result);
    requestNumber = json.args[0].node_id;
    body = unescape(json.body);

    console.log('received from queue.'+requestNumber);
    console.log('performing processing'+requestNumber);
}